package com.example.wallpaperapp.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.wallpaperapp.R;
import com.example.wallpaperapp.adapter.FragmentAdapte;
import com.example.wallpaperapp.base.BaseFragment;
import com.example.wallpaperapp.ui.activity.MomentAddActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;


public class FindFragment extends BaseFragment {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<Fragment> list;
    private FloatingActionButton floatingActionButton;

    @Override
    protected void initView(View view) {
        floatingActionButton = view.findViewById(R.id.floating_button);
        viewPager  = view.findViewById(R.id.find_viewpager);
        tabLayout = view.findViewById(R.id.find_tablayout);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find;
    }
    @Override
    protected void initData(Context context) {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_item_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_item_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.TabLayoutTextSize_two);
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        list = new ArrayList<>();
        list.add(new FindAttentionFragment());
        list.add(new FindHotFragment());
        //Fragment嵌套Fragment 用getChildFragmentManager
        FragmentAdapte adapter = new FragmentAdapte(list,getChildFragmentManager());
        viewPager.setAdapter(adapter);
        //联动
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText("关注");
        tabLayout.getTabAt(1).setText("热门");
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MomentAddActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {

    }

}